﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml.Linq;
using Gollie.DataTables;
using Gollie.WebCache;
using MySQL.Db;

namespace Gollie.Controls
{
	public partial class MyCredits : AuthenticationRequiredCustomerBaseUserControl
	{
		internal const string sessionOffersBoughtKey = "offersbought";

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.btnSearch_Click(null, null);
			this.CustomizeView();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			DataTables.MyCredits myCredits = new DataTables.MyCredits(this.UserInfo.Id);

			this.ddlVendors.DataTextField = "Key";
			this.ddlVendors.DataValueField = "Value";
			this.ddlVendors.DataSource = myCredits.vendors;
			this.ddlVendors.DataBind();
			this.ddlVendors.SelectedValue = "0";

			this.GridView1.DataSource = myCredits;

			this.GridView1.DataBind();

			this.CustomizeView();

			this.CreateOffersJustBoughtField();
		}

		private void CreateOffersJustBoughtField()
		{
			string cacheKey = sessionOffersBoughtKey + "_" + UserInfo.Id.ToString();
			object o = DataCache.Cache[cacheKey]; 
			if (o != null)
			{
				ResultSet r = o as ResultSet;

				DataCache.Cache.Remove(cacheKey);

				if (r.Count > 0)
				{
					XDocument xdoc = r.ToXml();
					this.hidJustBought.Text = xdoc.ToString();
					
					return;
				}
			}

			this.hidJustBought.Visible = false;
		}

		private void CustomizeView()
		{
			foreach (GridViewRow row in this.GridView1.Rows)
			{
				VendorPresenter vendorPresenter = LoadControl("VendorPresenter.ascx") as VendorPresenter;
				HiddenField v = row.Cells[0].FindControl("fldVendorId") as HiddenField;
				HiddenField b = row.Cells[0].FindControl("fldBedrijfsnaam") as HiddenField;
				HiddenField w = row.Cells[0].FindControl("fldWebsite") as HiddenField;
				HiddenField p = row.Cells[0].FindControl("fldPlaats") as HiddenField;
				HiddenField s = row.Cells[0].FindControl("fldStraat") as HiddenField;
				vendorPresenter.VendorId = Convert.ToInt32(v.Value);
				vendorPresenter.Bedrijfsnaam = b.Value;
				//vendorPresenter.Website = w.Value;
				vendorPresenter.Website = " ";
				vendorPresenter.City = p.Value;
				vendorPresenter.Street = s.Value;

				row.Cells[0].Controls.Add(vendorPresenter);
			}
		}


		protected void btnSearch_Click(object sender, ImageClickEventArgs e)
		{
			string filterGiven = this.ddlVendors.SelectedValue;

			foreach (GridViewRow row in this.GridView1.Rows)
			{
				if (filterGiven == "0")
				{
					row.Visible = true;
					continue;
				}

				HiddenField b = row.Cells[0].FindControl("fldVendorId") as HiddenField;
				row.Visible = b.Value == filterGiven;
			}

			return;
		}

		public SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			List<MyCreditsItem> list = new DataTables.MyCredits(this.UserInfo.Id).Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataSource = list;
			this.GridView1.DataBind();

			this.btnSearch_Click(null, null);
			this.CustomizeView();
/*
			DataView dv = new DataView(new DataTables.MyCredits(this.UserInfo.Id));

			dv.Sort = e.SortExpression + (SortDir == SortDirection.Ascending ? " ASC" : " DESC");
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			this.GridView1.DataSource = dv;
			this.GridView1.DataBind();

			this.btnSearch_Click(null, null);

			this.CustomizeView();
 */ 
		}

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				Control field = e.Row.Cells[0].FindControl("fldVendorId");
				if (field != null)
				{
					string backColor = "#F7F6F3";
					if ((e.Row.RowIndex & 1) == 1)
						backColor = "#ffffff";

					HiddenField hf = (HiddenField)field;
					string vendorId = hf.Value;

					e.Row.Attributes["onclick"] = "javascript:window.location.href = '../MyCreditsPerVendor.aspx?v=" + vendorId + "';";
					e.Row.Attributes["onmouseover"] = "javascript:this.style.cursor = 'pointer';  this.style.textDecoration='underline'; this.style.backgroundColor='#E2DEEA';";
					e.Row.Attributes["onmouseout"] = "javascript:this.style.textDecoration='none'; this.style.backgroundColor='" + backColor + "';";
				}

			}
		}
	}
}